﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;

using MT.eVotePRS.Device;
using MT.eVotePRS.Model;

using MT.eVotePRS.Presentation;
using MT.eVotePRS.Presentation.Logic;

namespace MT.eVotePRS.Presentation.Presenter
{
    public class RptIndividualPresenter
    {
        private IDeviceAPIHelper arsHelper;
        private IViewRptIndividual view;

        private ReportActions rptAction;

        public ReportActions RptAction
        {
            get { return rptAction; }
            set { rptAction = value; }
        }

        public void SetView(IViewRptIndividual view, IDeviceAPIHelper ars)
        {
            this.view = view;
            view.GenerateReport += OnGenerateReport;
            view.PropertyChanged += OnViewPropertyChanged;

            this.arsHelper = ars;
            this.arsHelper.DataReceived += OnDataReceived;
        }

        void OnGenerateReport(object sender, EventArgs e)
        {
            if (view.Report == IndividualReport.Handset)
            {
                rptAction.ShowHandsetEvents(view.HandsetId);
            }
            else if (view.Report == IndividualReport.Student)
            {
                rptAction.ShowStudentEvents(view.StudentId);
            }
        }

        void OnViewPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
        }

        void OnDataReceived(object sender, ARSEventArgs e)
        {
            HandsetData handsetData = (HandsetData)e.ReceivedData;
            switch (handsetData.CommandCode)
            {
                case ARSCommandCode.STUDENT_JOIN:
                     // TODO: Check Handset Status
                    if (view.Report == IndividualReport.Handset)
                    {
                        view.HandsetId = handsetData.MacStr;
                    }
                    else
                    {
                        view.StudentId = handsetData.UserID;
                    }

                    break;

                default:
                    break;
            }
        }

    }
}
